package com.example.a1.myapplication.service;

import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.os.StrictMode;

import com.example.a1.myapplication.constants.SocketConstants;
import com.example.a1.myapplication.socket.MinaCallBack;
import com.example.a1.myapplication.socket.NetProxy;
import com.example.a1.myapplication.utils.BroadcastUtils;

/**
 * @author 郭晓龙
 * @time on 2019/7/17
 * @email xiaolong.guo@inossem.com
 * @describe Socket服务
 */
public class SocketService extends Service {

    //ip地址
    private String socketUrl = SocketConstants.DEFULE_SOCKET_URL;
    //端口号
    private int socketPort = SocketConstants.DEFULE_SOCKET_PORT;
    //上下文
    private Context mContext;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        socketUrl = intent.getStringExtra(SocketConstants.SOCKET_URL);
        socketPort = intent.getIntExtra(SocketConstants.SOCKET_PORT, SocketConstants.DEFULE_SOCKET_PORT);
        initSocket();
        return START_STICKY;
    }

    /**
     * Socket初始化
     */
    private void initSocket() {
        //打开TCP连接
        boolean result = NetProxy.getConnect(socketUrl, socketPort, new MySocketCallBack());
        if (result) {
            //发送数据
//            NetProxy.send("123");
        } else {
            try {
                //连接失败10秒重连
                Thread.sleep(SocketConstants.RECONNECT_TIME);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            initSocket();
        }
    }

    /**
     * Socket回调接口
     * messageReceived 收到消息回调
     * sessionClosed 连接断开回调
     */
    private class MySocketCallBack implements MinaCallBack {

        @Override
        public void messageReceived(String msg) {
            BroadcastUtils.getInstance(mContext).sendBroadcast(SocketConstants.SOCKET_ACTION, new String[]{SocketConstants.SOCKET_MESSAGE}, new Object[]{msg});
        }

        @Override
        public void sessionClosed(boolean isCon) {
            try {
                //掉线10秒重连
                Thread.sleep(SocketConstants.RECONNECT_TIME);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            initSocket();
        }
    }

    @Override
    public ComponentName startService(Intent service) {
        return super.startService(service);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        try {
            Intent intent = new Intent(this, SocketService.class);
            startService(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}